package nix.demo6.web.filter;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

public class EncodingFilter implements Filter {

	public void destroy() {

	}

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

		// 处理POST请求编码问题
		request.setCharacterEncoding("utf-8");
				
		HttpServletRequest req = (HttpServletRequest)request;
		
		if (req.getMethod().equals("POST")) {
			chain.doFilter(request, response);
		} else if (req.getMethod().equals("GET")) {
			// 处理GET请求编码问题
			EncodingRequest ereq = new EncodingRequest(req);
			chain.doFilter(ereq, response);
		}	
	}

	public void init(FilterConfig fConfig) throws ServletException {

	}

}
